package com.bwei.topnews.view;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.view.PagerAdapter;
import android.util.AttributeSet;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bwei.topnews.R;
import com.bwei.topnews.utils.CommonUtils;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

/**
 * Created by qwe on 2016/11/1.
 */
public class ViewPagerIndicator extends HorizontalScrollView implements View.OnClickListener, LazyViewPager.OnPageChangeListener {
    private LazyViewPager viewPager;
    private LinearLayout linearLayout;
    private SlidingMenu slidingMenu;

    /**
     * new 时候用
     * @param context
     */
    public ViewPagerIndicator(Context context) {
        super(context);
        init();
    }

    /**
     * 初始化操作
     */

    private void init() {
    }

    /**
     * 布局里边引用
     * @param context
     * @param attrs
     */

    public ViewPagerIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ViewPagerIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void setViewPager(LazyViewPager viewPager, SlidingMenu slidingMenu){
        this.viewPager = viewPager;
        this.slidingMenu = slidingMenu;
        this.removeView(linearLayout);
        PagerAdapter adapter = viewPager.getAdapter();

        // 因为HorizontalScrollView只可以有一个子控件
        // 定义一个LinearLaout来装在title
        linearLayout = new LinearLayout(getContext());
        for (int i = 0; i < adapter.getCount(); i++) {
            View view = CommonUtils.inflate(R.layout.view);
            view.setOnClickListener(this);
            TextView tv_indicator_line = (TextView) view.findViewById(R.id.tv_indicator_line);
            TextView tv_indicator_title = (TextView) view.findViewById(R.id.tv_indicator_title);
            if(i==0){
                tv_indicator_line.setVisibility(View.VISIBLE);
                tv_indicator_title.setTextColor(Color.RED);
            }else{
                tv_indicator_line.setVisibility(View.INVISIBLE);
                tv_indicator_title.setTextColor(Color.GRAY);
            }
            tv_indicator_title.setText(adapter.getPageTitle(i));
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            params.setMargins(CommonUtils.dip2px(5),CommonUtils.dip2px(5),CommonUtils.dip2px(5),CommonUtils.dip2px(5));
            linearLayout.addView(view,params);
        }
            this.addView(linearLayout);

        viewPager.setOnPageChangeListener(this);


    }

    @Override
    public void onClick(View v) {

        for (int i = 0; i < linearLayout.getChildCount(); i++) {
            View view = linearLayout.getChildAt(i);
            if(view == v){
                viewPager.setCurrentItem(i);
            }
        }

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        
    }

    @Override
    public void onPageSelected(int position) {

        for (int i = 0; i < linearLayout.getChildCount(); i++) {
            View view = linearLayout.getChildAt(i);
            TextView  tv_indicator_title= (TextView) view.findViewById(R.id.tv_indicator_title);
            TextView  tv_indicator_line= (TextView) view.findViewById(R.id.tv_indicator_line);
            if(i == position){
                this.scrollTo((int) (view.getX()-linearLayout.getChildAt(0).getWidth()),0);
                tv_indicator_line.setVisibility(View.VISIBLE);
                tv_indicator_title.setTextColor(Color.RED);
            }else{
                tv_indicator_line.setVisibility(View.INVISIBLE);
                tv_indicator_title.setTextColor(Color.GRAY);
            }

        }

        if(position == 0){
            slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        }else{
            slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
        }

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
